package com.kawaiim.zhj.entity;

import android.graphics.drawable.Drawable;
import android.view.View;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.kawaiim.zhj.LandlordActivity;
import com.kawaiim.zhj.R;
import com.kawaiim.zhj.GameAnimation.CardAnimator;
import com.kawaiim.zhj.OutCard.OutCardStyleEnum;
import com.kawaiim.zhj.utilities.Constants;

//玩家手中牌的类
public class Player {
    public  String name = null;    //玩家姓名
    public int image;      //玩家图片
    public int playIndex = -1;       //当前玩家显示的位置 找到名字设置方向

    public List<Card> cards = new ArrayList<Card>();    //玩家的卡组

    public int integration = 1000;            //积分
    public CardOrderMode orderMode;    //牌序方式，0是从大到小排，1是按牌的个数排

    public boolean isRemind;           // 是否点了提示
    public boolean isRedA =false;         //是否是红箭
    public boolean isRedThree;         //是否红三
    public boolean isWin = false;              //是否出完牌了
    public int outCardNum;          //出过牌的次数

    public List<Card> htCards = new ArrayList<Card>();       //提示能出的牌
    public OutCardStyleEnum htStyle;
    public boolean isOutByMy;        //提示的时候是不是自己出牌
    public boolean canOutCard;      //记录是否可以出牌

    LandlordActivity game;           //绑定对应的游戏activity


    //初始化
    public Player(LandlordActivity landlordActivity) {
        game = landlordActivity;
    }

    public Player() {
        Init_AfterTurn();
    }

    //一局以后清空的函数
    public void Init_AfterTurn() {

        htStyle = OutCardStyleEnum.CANT_OUT;
        htCards.clear();
        orderMode = CardOrderMode.CARD_SIZE;
        isRemind = false;
        outCardNum = 0;           //初始化没出过牌
        isWin = false;               //初始化为输
        canOutCard = false;         //初始化为不能
        isRedA=false;
    }

    //更改hintResult
    public void UpdateHintResult() {
        isRemind = false;
        htCards.clear();
        htStyle = OutCardStyleEnum.CANT_OUT;
        canOutCard = false;
    }


    //设置当前玩家的图片
    public void setImage(int image) {
        //如果图片超出范围限制为1
        if (image < Constants.MIN_IMAGE || image > Constants.MAX_IMAGE) {
            image = Constants.MIN_IMAGE;
        }

        this.image = image;
        Field field = null;
        int id = 0;
        try {
            //如果图片不合法，设置为机器人图片
            if (image > Constants.MAX_IMAGE) {
                id = R.mipmap.big_robot;
            } else {
                field = R.mipmap.class.getField("women" + Integer.toString(image));
                id = field.getInt(null);
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        final int finalId = id;
        game.peoplesImage[playIndex].setBackground(game.getResources().getDrawable(finalId, game.getTheme()));


    }

    //设置玩家的姓名
    public void setName(String name) {
        this.name = name;
        game.mes[playIndex].setText(name);

    }
    public void setName(String name,int color) {

        this.name = name;
        LandlordActivity.mes[playIndex].setText(name);
        LandlordActivity.mes[playIndex].setTextColor(color);


    }


    //设置当前玩家的动作（开始，一分，二分，三分，不叫，不出）
    // 淡入
    // 配音
    public void setAction(PeopleActionEnum peopleActionEnum) {
        Drawable btnDrawable = null;

        //根据玩家的动作挑选图片
        if (Objects.requireNonNull(peopleActionEnum) == PeopleActionEnum.REFUSED) {
            btnDrawable = game.getResources().getDrawable(R.mipmap.refuse_pic, game.getTheme());
            //播放不出的音效
            game.soundEffect.Player_Refuse_Voice();
        } else {
            return;
        }

        //设置图片并将其淡入
        game.four_action_pic[playIndex].setBackground(btnDrawable);
        game.four_action_pic[playIndex].setVisibility(View.VISIBLE);
        CardAnimator.alphaRun(game.four_action_pic[playIndex], Constants.LIGHT_DURATION_TIME);
    }

    //将当前的动作显示淡出
    public synchronized void actionAlphaGoneRun() {
        if (View.VISIBLE == game.four_action_pic[playIndex].getVisibility())
        {
            CardAnimator.alphaGoneRun(game.four_action_pic[playIndex], Constants.LIGHT_DURATION_TIME);
        }
    }

}
